/**
 * 
 */
var Mod_Draw = {
	/**
	 * 绘制首页睡眠图表
	 * 由于数据格式改变，所以采用单独的方法绘制首页睡眠的树状图
	 * @param width
	 * @param height
	 * @param canvas
	 * @param vLineColor
	 * @param hLineColor
	 * @param textColor
	 * @param treeDate
	 * @author wangguang
	 */
	drawTreeToHomeSleep: function(width,height,canvas,vLineColor,hLineColor,textColor,treeDate){
		//width 160px height 75
		canvas.width = width;
		canvas.height = height;
		var ctx = canvas.getContext("2d");
		// console.log("width:" + width + "---height:" + height);
		var xTexts = treeDate.xLables.split(",");
		var yValues = treeDate.yValues.split(",");
		
		//第一条线位置	
		var lineXone = width * 0.025;	
		//底部文字位置
		var xTextIndex = 7.5;
		var hLineMaxHeight = 50;
		var hLineY = 20;
		var vLineWidth = 6;
		var hLineWidth = 2;
	
		//设置高度比例
		xTextIndex = height * xTextIndex / 75;
		hLineMaxHeight = height * hLineMaxHeight / 75;
		hLineY = height * hLineY / 75;
	
		//底部文字
		ctx.beginPath();
		ctx.fillStyle = textColor;
		ctx.font = '26px Helvetica';
		ctx.textBaseline = "middle";
		ctx.textAlign = 'center';
	
		var textW = width * 0.95 / xTexts.length;
	
		for(var i = 0; i < xTexts.length; i++){
			ctx.fillText(xTexts[i], lineXone + textW * i + ctx.measureText(xTexts[i]).width ,height - xTextIndex);
		}
		ctx.closePath();
		
		if(treeDate.weekSleep > 0){
			//树状图最大值
			var yValuesMax = Math.max.apply(null,yValues);
			yValuesMax = yValuesMax > treeDate.hLine ? yValuesMax : treeDate.hLine;
			//填充树状图
			ctx.beginPath();
			ctx.strokeStyle = vLineColor;
			ctx.lineWidth = vLineWidth;
			
			for(var i = 0; i < yValues.length; i++){
				ctx.moveTo(lineXone + textW * i + ctx.measureText(xTexts[i]).width,height - hLineY);
				ctx.lineTo(lineXone + textW * i + ctx.measureText(xTexts[i]).width,height - hLineY - yValues[i] / yValuesMax * hLineMaxHeight);
			}
			ctx.closePath();
			ctx.stroke();
			
			//绘制目标线
			if(treeDate.hLine > 0){
				this.drawHDashed(ctx,hLineColor,hLineWidth,width * 0.025,width * 0.975, height - hLineY - treeDate.hLine / yValuesMax * hLineMaxHeight,30);
			}
		} else {
			//没数据的时候
			if(treeDate.hLine > 0){
	
				ctx.beginPath();
				ctx.fillStyle = textColor;
				ctx.font = "30px Helvetica";
				ctx.textBaseline = "bottom";
				ctx.textAlign = "center";
				ctx.fillText(treeDate.nullTips,width / 2,height / 1.5);
				ctx.closePath();
				//绘制目标线
				this.drawHDashed(ctx,hLineColor,hLineWidth,width * 0.025,width * 0.975, height / 3,30);
	
			} else {
				ctx.beginPath();
				ctx.fillStyle = textColor;
				ctx.font = "30px Helvetica";
				ctx.textBaseline = "bottom";
				ctx.textAlign = "center";
				ctx.fillText(treeDate.nullTips,width / 2,height - hLineY);
				ctx.closePath();	
			}				
		}
	},
	//绘制横线虚线
	drawHDashed: function(ctx,lineColor,lineWidth,xStart,xEnd,yIndex,lienCount){
		ctx.beginPath();
		ctx.strokeStyle = lineColor;
		ctx.lineWidth = lineWidth;			
		//横线总长度
		var hLineLength = xEnd - xStart;
		for(var i = 0; i < lienCount; i++ ){
			if(i % 2 == 0){
				ctx.moveTo(xStart + i * hLineLength / lienCount,yIndex);
				ctx.lineTo(xStart + (i + 1) * hLineLength / lienCount,yIndex);	
			}
		}
		ctx.closePath();
		ctx.stroke();
	}
};
